package com.swak.frame.dto;


import com.swak.frame.dto.base.VO;
import com.swak.frame.enums.BasicErrCode;
import com.swak.frame.enums.IResultCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 基础结果Result
 *
 * @author colley.ma
 * @since 2022/9/9 16:17
 */
@Setter
@Getter
@Accessors(chain = true)
public class Result<T> implements VO {
    private static final long serialVersionUID = 7885784556539697892L;
    private Integer code;
    private String msg;
    private T data;

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Result(T data) {
        this();
        this.data = data;
    }
    public Result(IResultCode errCode) {
        this(errCode.getCode(), errCode.getI18nMsg());
    }

    public Result(IResultCode errCode,Object...args) {
        this(errCode.getCode(), errCode.getI18nMsg(args));
    }

    public Result() {
        this(BasicErrCode.SUCCESS);
    }

    public boolean isSuccess() {
        return BasicErrCode.SUCCESS.eq(getCode());
    }

    public static <T>  Result<T> builder(IResultCode errCode) {
       return new Result<>(errCode);
    }

    public  static <T> Result<T> fail(Integer code, String msg) {
          return new Result<>(code,msg);
    }

    public  static <T> Result<T> fail(IResultCode resultCode,Object ... args) {
       return new Result<>(resultCode,args);
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }
}